package com.doit.day03;

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.record.TimestampType;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class _01_再均衡监听测试1 {
    public static void main(String[] args) {
//        KafkaConsumer<String, String> consumer = KafkaConsumerUtils.getConsumer("java", "test");
        Map<String, Object> map = new HashMap<>();
        //必须配置的
        map.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        map.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
        map.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        map.put(ConsumerConfig.GROUP_ID_CONFIG,"g02");
        //选配的
        map.put(ConsumerConfig.ALLOW_AUTO_CREATE_TOPICS_CONFIG,"true");
        //设置从哪里开始读取数据  latest  最新的
        map.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"latest");
        //允许自动提交偏移量  __consumer_offsets
        map.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");

        map.put(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, CooperativeStickyAssignor.class.getName());
        //自动提交偏移量的间隔时间
//        map.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"10000");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(map);
        //告诉他，干啥呀？消费哪一个topic中的数据
        consumer.subscribe(Arrays.asList("reb-1", "reb-2"), new ConsumerRebalanceListener() {
            /**
             * 这个方法指的是回收之前的分区情况
             * @param partitions
             */
            @Override
            public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
                System.out.println("我回收之前消费的分区是："+partitions);
            }

            /**
             * 回收之后再分配完成时，我消费的分区情况
             * @param partitions
             */
            @Override
            public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
                System.out.println("我是再分配之后的情况："+partitions);
            }
        });        //消费数据
        while (true){
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
        }



    }
}
